//
//  HelloWorldScene.m
//
//  Created by : caominglei
//  Project    : Skip
//  Date       : 2018/2/11
//
//  Copyright (c) 2018年 caominglei.
//  All rights reserved.
//
// -----------------------------------------------------------------

#import "HomeScene.h"
#import "OALSimpleAudio.h"

// -----------------------------------------------------------------------
@interface HomeScene ()
{
    CCSprite *_lighting;
}
@end
@implementation HomeScene

// -----------------------------------------------------------------------

- (id)init
{
    // Apple recommend assigning self with supers return value
    self = [super init];
    
    // The thing is, that if this fails, your app will 99.99% crash anyways, so why bother
    // Just make an assert, so that you can catch it in debug
    NSAssert(self, @"Whoops");
    /*
    // Background
    CCSprite9Slice *background = [CCSprite9Slice spriteWithImageNamed:@"white_square.png"];
    background.anchorPoint = CGPointZero;
    background.contentSize = [CCDirector sharedDirector].viewSize;
    background.color = [CCColor grayColor];
    [self addChild:background];
    
    // The standard Hello World text
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"ArialMT" fontSize:64];
    label.positionType = CCPositionTypeNormalized;
    label.position = (CGPoint){0.5, 0.5};
    [self addChild:label];
     */
    [self createBg_image];
    [self createGamePlayers];
    [self createLighting];
//    [[OALSimpleAudio sharedInstance] playBg:@"bg_sound.mp3"];    // done
    [self createMenu];

    return self;
}

// -----------------------------------------------------------------------
- (void)createBg_image
{
    CCSprite *bg_img = [CCSprite spriteWithImageNamed:@"girl_bg.jpg"];
    bg_img.anchorPoint = CGPointZero;
    [self addChild:bg_img];
    
}

- (void)createGamePlayers
{
    //加载纹理相册,当纹理对象多时该方法一次性渲染纹理的作用-优化性能就凸显出来了
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@""];
    CGSize winSize = [CCDirector sharedDirector].viewSize;
    CCSprite *left = [CCSprite spriteWithImageNamed:@"left.jpg"];
    CGFloat leftX = 60;
    left.position = ccp(leftX, winSize.height * 0.5);
    [self addChild:left];
    
    
    CCSprite *right = [CCSprite spriteWithImageNamed:@"right.jpg"];
    right.position = ccp(winSize.width - leftX, winSize.height * 0.5);
    [self addChild:right];
    

}

- (void)createLighting
{
    CCSprite *light_img = [CCSprite spriteWithImageNamed:@"lighting.jpg"];
    light_img.anchorPoint = CGPointZero;
    //闪电由隐藏到展示 透明度0——>255，使用消息调度来实现这个过程效果
    light_img.opacity = 0;
    [self addChild:light_img];
    _lighting = light_img;
    [self lightingEffect];
    //消息调度
    [self schedule:@selector(lightingEffect) interval:6];
}

- (void)lightingEffect
{
    //显示出来
    _lighting.opacity = 255;
    //闪烁
    CCActionBlink *blink = [CCActionBlink actionWithDuration:1 blinks:5];
    //淡出
    CCActionFadeOut *fadeOut = [CCActionFadeOut actionWithDuration:2];
    CCActionSequence *action = [CCActionSequence actionOne:blink two:fadeOut];
    [_lighting runAction:action];
    //音效
    [[OALSimpleAudio sharedInstance] playEffect:@"lighting.mp3"];
}

- (void)createMenu
{
    UIMenuItem *start = [[UIMenuItem alloc] initWithTitle:@"游戏开始" action:@selector(menuItemClick:)];
    UIMenuItem *readSave = [[UIMenuItem alloc] initWithTitle:@"读取存档" action:@selector(menuItemClick:)];
    UIMenuItem *setting = [[UIMenuItem alloc] initWithTitle:@"游戏设置" action:@selector(menuItemClick:)];
    UIMenuItem *help = [[UIMenuItem alloc] initWithTitle:@"游戏帮助" action:@selector(menuItemClick:)];

    [UIMenuController sharedMenuController].menuItems = @[start,readSave,setting,help];
    [UIMenuController sharedMenuController].menuVisible = YES;
     
}

- (void)menuItemClick:(UIMenuItem *)item
{
    
}
@end























// why not add a few extra lines, so we dont have to sit and edit at the bottom of the screen ...
